#include "inquiresql.h"

#include "enumtypeheader.h"

#include <QtSql>
#include <QString>
#include <QStringList>
#include <QSqlQuery>
#include <QDebug>

InquireSql::InquireSql()
{
}

QStringList InquireSql::inquire_staff_ID() const
{
    QSqlQuery query;
    QString queryString = QString("Select staff_id From staff_management");

    QStringList name_list;
    if(query.exec(queryString)){

        while(query.next()){
            QString name = query.value(0).toString();
            name_list.append(name);
        }
    }
    return name_list;
}

QStringList InquireSql::inquireAllStaffName() const
{
    QSqlQuery query;
    QString queryString = QString("Select name From staff_management");

    QStringList name_list;
    if(query.exec(queryString)){

        while(query.next()){
            QString name = query.value(0).toString();
            name_list.append(name);
        }
    }
    return name_list;
}

QString InquireSql::InquireStaffName(const QString &staffID) const
{
    QSqlQuery query;
    QString queryString = QString("Select name From staff_management where staff_id = '%1'")
                                                    .arg(staffID);

    if(query.exec(queryString))
    {

        while(query.next()){
            QString name = query.value(0).toString();
            return name;
        }
    }

    return NULL;
}

QString InquireSql::InquireStaffGender(const QString &staffID) const
{
    QSqlQuery query;
    QString queryString = QString("Select gender From staff_management where staff_id = '%1'")
                                                    .arg(staffID);

    if(query.exec(queryString))
    {

        while(query.next()){
            QString gender = query.value(0).toString();
            return gender;
        }
    }

    return NULL;
}

QString InquireSql::InquireStaffID(QString name) const
{
    QSqlQuery query;
    QString queryString = QString("Select staff_id From staff_management where name = '%1'")
                                                    .arg(name);

    if (query.exec(queryString))
    {
        while(query.next()){
            QString staffID = query.value(0).toString();
            return staffID;
        }
    }
    return NULL;
}

QString InquireSql::inquire_staff_password(const QString str) const
{
    QSqlQuery query;
    QString queryString = QString("Select password from staff_management "
                                  " where staff_id='%1' ").arg(str);
    QString paswd;

    if(query.exec(queryString)){

        while(query.next()){
            paswd = query.value(0).toString();
        }
    }
    return paswd;
}

bool InquireSql::UpdatStaffMemberRecharge(const QString &staffName, const QString &amount) const
{
    QSqlQuery query;
    QString queryString;
    QString staffID;

    queryString = QString(" select staff_id from staff_management "
                        " where name = '%1' ").arg(staffName);
    if(query.exec(queryString))
    {
        while(query.next()){
            staffID = query.value(0).toString();
        }
    }

    queryString = QString(" insert into staff_result values "
                          " ('%1', '1', '%2', '0', '0.00', '0.00','0.00', '%3') ")
            .arg(staffID, amount, QDateTime::currentDateTime().toString("yyyy-MM-dd"));

    if(query.exec(queryString))
        return true;
    else
    {
        QString membercardRechargeTimes, membercardAmount;
        queryString = QString(" select membercard_recharge_times, membercard_amount from staff_result "
                              " where staff_id = '%1' and result_date = '%2' ")
                .arg(staffID, QDateTime::currentDateTime().toString("yyyy-MM-dd"));
        if(query.exec(queryString))
        {
            while(query.next()){
                membercardRechargeTimes = query.value(0).toString();
                membercardAmount = query.value(1).toString();
            }
        }

        queryString = QString(" update staff_result set membercard_recharge_times = '%3', "
                              " membercard_amount = '%4' "
                              " where staff_id = '%1' and result_date = '%2' ")
                .arg(staffID,
                     QDateTime::currentDateTime().toString("yyyy-MM-dd"),
                     QString::number(membercardRechargeTimes.toInt() + 1),
                     QString::number(amount.toDouble() + membercardAmount.toDouble()));

        if(!query.exec(queryString))
        {
            qDebug() << "UpdatStaffMemberRecharge no exec!";
            return false;
        }
    }
    return true;
}

staffList InquireSql::InquireStaffResultList(QString startDate, QString endDate) const
{
    staffList list;
    QSqlQuery query;
    QString queryString;

    if (startDate == "all")
//        queryString = QString(" select * from staff_result ");
        queryString = QString(" select staff_id, sum(membercard_recharge_times), "
                              " sum(membercard_amount), sum(sale_quantity), "
                              " sum(total_sales), sum(cost), sum(profit) "
//                              " from (select * from staff_result) "
                              " from staff_result "
                              " group by staff_id ");
    else if (startDate == endDate)
        queryString = QString(" select * from staff_result where result_date between '%1' and '%2' ")
                                            .arg(startDate, endDate);
    else
        queryString = QString(" select staff_id, sum(membercard_recharge_times ), "
                              " sum(membercard_amount), sum(sale_quantity), "
                              " sum(total_sales), sum(cost), sum(profit)"
                              " from (select * from staff_result where result_date between '%1' and '%2') "
                              " group by staff_id ")
                                .arg(startDate, endDate);

    if(query.exec(queryString))
    {
        while(query.next()){
            QString staffID                 = query.value(0).toString();
            QString staffName               = InquireStaffName(query.value(0).toString());
            QString gender                  = InquireStaffGender(query.value(0).toString());
            QString rechargeTimes           = query.value(1).toString();
            QString rechargeAmount          = query.value(2).toString();
            QString saleQuantity            = query.value(3).toString();
            QString totalSales              = query.value(4).toString();
            QString cost                    = query.value(5).toString();
            QString profit                  = query.value(6).toString();

            PersonStaff temp(staffID, staffName, gender, rechargeTimes,
                                   rechargeAmount, saleQuantity, totalSales,
                                   cost, profit);
            list.append(temp);
        }
    }
    return list;
}

bool InquireSql::UpdateStaffPassword(const QString &staffcardID, const QString &staffPassword) const
{
    QSqlQuery query;
    QString queryString = QString(" update staff_management set "
                                  " password = '%2' "
                                  " where staff_id = '%1' ").arg(staffcardID, staffPassword);

    if(!query.exec(queryString))
        return false;
    return true;
}

staffInformationList InquireSql::InquireStaffInformationList() const
{
    staffInformationList list;
    QSqlQuery query;
    QString queryString;

    queryString = QString("select * from staff_management");
    if(query.exec(queryString))
    {
        while(query.next()){
            QString staffID =        query.value(0).toString();
            QString staffName =      query.value(2).toString();
            QString staffGender =    query.value(3).toString();
            QString staffBirthday =  query.value(4).toString();
            QString entryDate =      query.value(6).toString();
            QString staffAge = QString::number(
                        QDateTime::currentDateTime().toString("yyyy").toInt()
                        - query.value(4).toString().left(4).toInt());
            QString wage =           query.value(7).toString();
            QString staffTel =       query.value(8).toString();
            QString staffAdress =    query.value(9).toString();
            QString remark =         query.value(10).toString();

            PersonStaffInformation temp(staffID, staffName, staffGender,
                                staffBirthday, entryDate, staffAge, wage,
                                staffTel, staffAdress, remark);
            list.append(temp);
        }
    }
    return list;
}

bool InquireSql::InsertStaffInformation(const PersonStaffInformation &staffInformation) const
{
    QSqlQuery query;
    QString queryString, queryString2;

    //插入员工信息  初始密码为员工号
    queryString = QString( " insert into staff_management "
                           " values ('%1', '%2', '%3', '%4', '%5', '%6', " )
                                  .arg(staffInformation.get_personID(),
                                       staffInformation.get_personID(),
                                       staffInformation.get_personName(),
                                       staffInformation.get_personGender(),
                                       staffInformation.get_personBirthday(),
                                       staffInformation.get_personAge());

    queryString2 = QString(" '%1', '%2', '%3', '%4', '%5') ")
                                  .arg(staffInformation.get_entryDate(),
                                       staffInformation.get_wage(),
                                       staffInformation.get_personTel(),
                                       staffInformation.get_personAdress(),
                                       staffInformation.get_remark());

    if(!query.exec(queryString+queryString2))
    {
        qDebug() << "InsertStaffInformation no exec!";
        return false;
    }

    queryString = QString( " insert into staff_result values ( "
                           " '%1', '0', '0.00', '0', '0.00', '0.00', '0.00', '%2') " )
                          .arg(staffInformation.get_personID(),
                               QDateTime::currentDateTime().toString("yyyy-MM-dd"));

    qDebug() << queryString;
    if(!query.exec(queryString))
    {
        qDebug() << "InitStaffResult no exec!";
        return false;
    }

    return true;
}

bool InquireSql::UpdateStaffInformation(const QString &staffID,
                 const PersonStaffInformation &staffInformation) const
{
    DeleteStaffInformation(staffID);
    return InsertStaffInformation(staffInformation);
}

void InquireSql::DeleteStaffInformation(const QString &staffID) const
{
    QSqlQuery query;
    QString queryString;
    queryString = QString(" delete from staff_management "
                          " where staff_id = '%1' ").arg(staffID);

    if(!query.exec(queryString))
    {
        qDebug() << "DeleteStaffInformation no exec!";
    }
}

bool InquireSql::UpdatStaffSalesResult(const StaffResultClass &staffResultClass) const
{
    QSqlQuery query;
    QString queryString;

    queryString = QString(" insert into staff_result values "
                          " ('%1', '0', '0.00', '%2', '%3', '%4','%5', '%6') ")
            .arg(staffResultClass.get_staffID(),
                 staffResultClass.get_saleQuantity(),
                 staffResultClass.get_totalSales(),
                 staffResultClass.get_cost(),
                 staffResultClass.get_profit(),
                 QDateTime::currentDateTime().toString("yyyy-MM-dd"));

    if(query.exec(queryString))
        return true;
    else
    {
        QString saleQuantity, totalSales, cost, profit;
        queryString = QString(" select sale_quantity, total_sales, cost, profit from staff_result "
                              " where staff_id = '%1' and result_date = '%2' ")
                .arg(staffResultClass.get_staffID(),
                     QDateTime::currentDateTime().toString("yyyy-MM-dd"));
        if(query.exec(queryString))
        {
            while(query.next()){
                saleQuantity    = query.value(0).toString();
                totalSales      = query.value(1).toString();
                cost            = query.value(2).toString();
                profit          = query.value(3).toString();
            }
        }

        queryString = QString(" update staff_result set sale_quantity = '%3', "
                              " total_sales = '%4', "
                              " cost = '%5', "
                              " profit = '%6' "
                              " where staff_id = '%1' and result_date = '%2' ")
                .arg(staffResultClass.get_staffID(),
                     QDateTime::currentDateTime().toString("yyyy-MM-dd"),
                     QString::number(saleQuantity.toInt()
                        + staffResultClass.get_saleQuantity().toInt()),
                     QString::number(totalSales.toDouble()
                        + staffResultClass.get_totalSales().toDouble()),
                     QString::number(cost.toDouble()
                        + staffResultClass.get_cost().toDouble()),
                     QString::number(profit.toDouble()
                        + staffResultClass.get_profit().toDouble()));

        if(!query.exec(queryString))
        {
            qDebug() << "UpdatStaffSalesResult no exec!";
            return false;
        }
    }
    return true;
}

QStringList InquireSql::inquireOwnerName() const
{
    QSqlQuery query;
    QString queryString = QString("Select owner_name From supplier");

    QStringList ownerNameList;
    if(query.exec(queryString)){

        while(query.next()){
            QString ownerName = query.value(0).toString();
            ownerNameList.append(ownerName);
        }
    }
    return ownerNameList;
}

QStringList InquireSql::inquireAllAttributeValue(int current_page) const
{
    QSqlQuery query;
    QString queryString;
    switch (current_page)
    {
        case SpecificationPage:
            queryString = QString("select specifications from specifications_set");
            break;
        case BrandPage:
            queryString = QString("select brand from brand_set");
            break;
        case ColorPage:
            queryString = QString("select color from color_set");
            break;
        case UnitPage:
            queryString = QString("select unit from unit_set");
            break;
        default:
            break;
    }

    QStringList attributeValueList;
    if(query.exec(queryString)){

        while(query.next()){
            QString attributeValue = query.value(0).toString();
            attributeValueList.append(attributeValue);
        }
    }
    return attributeValueList;
}

attributelist InquireSql::InquireAttribute(int current_page) const
{
    attributelist list;
    QSqlQuery query;
    QString queryString;

    switch (current_page)
    {
        case SpecificationPage:
            queryString = QString("select * from specifications_set");
            break;
        case BrandPage:
            queryString = QString("select * from brand_set");
            break;
        case ColorPage:
            queryString = QString("select * from color_set");
            break;
        case UnitPage:
            queryString = QString("select * from unit_set");
            break;
        case categoryPage:
            queryString = QString("select * from category_set");
            break;
        default:
            break;
    }

    if(query.exec(queryString))
    {
        while(query.next()){
            QString attribute_number = query.value(0).toString();
            QString attribute = query.value(1).toString();

            CommodityAttribute temp(attribute_number, attribute);
            list.append(temp);
        }
    }
    return list;
}

void InquireSql::InsertAttribute(int current_page, const CommodityAttribute &attribute) const
{
    QSqlQuery query;
    QString queryString;

    switch (current_page)
    {
        case SpecificationPage:
            queryString= QString(" insert into specifications_set values('%1', '%2')")
                                                      .arg(attribute.get_attribute_number(),
                                                           attribute.get_attribute());
            break;
        case BrandPage:
            queryString = QString(" insert into brand_set values('%1', '%2')")
                    .arg(attribute.get_attribute_number(),
                         attribute.get_attribute());
            break;
        case ColorPage:
            queryString = QString(" insert into color_set values('%1', '%2')")
                    .arg(attribute.get_attribute_number(),
                         attribute.get_attribute());
            break;
        case UnitPage:
            queryString = QString(" insert into unit_set values('%1', '%2')")
                    .arg(attribute.get_attribute_number(),
                         attribute.get_attribute());
            break;
        case categoryPage:
            queryString = QString(" insert into category_set values('%1', '%2')")
                    .arg(attribute.get_attribute_number(),
                         attribute.get_attribute());
            break;
        default:
            break;
    }

    if(!query.exec(queryString))
    {
        qDebug() << "InsertAttribute no exec!";
    }
}

void InquireSql::UpdateAttribute(int current_page, const CommodityAttribute &attribute) const
{
    QSqlQuery query;
    QString queryString;

    switch (current_page)
    {
        case SpecificationPage:
            queryString = QString(" update specifications_set set specifications ='%2' "
                                      " where specifications_number = '%1' ")
                                                        .arg(attribute.get_attribute_number(),
                                                             attribute.get_attribute());
            break;
        case BrandPage:
            queryString = QString(" update brand_set set brand ='%2' "
                                  " where brand_number = '%1' ")
                                                    .arg(attribute.get_attribute_number(),
                                                         attribute.get_attribute());
            break;
        case ColorPage:
            queryString = QString(" update color_set set color ='%2' "
                                  " where color_number = '%1' ")
                                                    .arg(attribute.get_attribute_number(),
                                                         attribute.get_attribute());
            break;
        case UnitPage:
            queryString = QString(" update unit_set set unit ='%2' "
                                  " where unit_number = '%1' ")
                                                    .arg(attribute.get_attribute_number(),
                                                         attribute.get_attribute());
            break;
        case categoryPage:
            queryString = QString(" update category_set set category ='%2' "
                                      " where category_number = '%1' ")
                                                        .arg(attribute.get_attribute_number(),
                                                             attribute.get_attribute());
            break;
        default:
            break;
    }

    if(!query.exec(queryString))
    {
        qDebug() << "UpdateAttribute no exec!";
    }
}

void InquireSql::DeleteAttribute(int current_page, const QString &attribute_number) const
{
    QSqlQuery query;

    QString queryString;
    switch (current_page)
    {
        case SpecificationPage:
            queryString = QString(" delete from specifications_set "
                                      " where specifications_number = '%1' ").arg(attribute_number);
            break;
        case BrandPage:
            queryString = QString(" delete from brand_set "
                                      " where brand_number = '%1' ").arg(attribute_number);
            break;
        case ColorPage:
            queryString = QString(" delete from color_set "
                                      " where color_number = '%1' ").arg(attribute_number);
            break;
        case UnitPage:
            queryString = QString(" delete from unit_set "
                                      " where unit_number = '%1' ").arg(attribute_number);
            break;
        case categoryPage:
            queryString = QString(" delete from category_set "
                                      " where category_number = '%1' ").arg(attribute_number);
            break;
        default:
            break;
    }

    if(!query.exec(queryString))
    {
        qDebug() << "DeleteAttribute no exec!";
    }
}

bool InquireSql::AttributeIsUsed(int current_attribute, const QString &attributeNumber) const
{
    QSqlQuery query;
    QString queryString;

    switch (current_attribute)
    {
        case SpecificationPage:
            queryString = QString(" select count(*) from commodity_information "
                                " where specifications_number  = '%1' ").arg(attributeNumber);
            break;
        case BrandPage:
            queryString = QString(" select count(*) from commodity_information "
                                " where brand_number  = '%1' ").arg(attributeNumber);
            break;
        case ColorPage:
            queryString = QString(" select count(*) from commodity_information "
                                " where color_number = '%1' ").arg(attributeNumber);
            break;
        case UnitPage:
            queryString = QString(" select count(*) from commodity_information "
                                " where unit_number  = '%1' ").arg(attributeNumber);
            break;
        case categoryPage:
            queryString = QString(" select count(*) from commodity_information "
                                " where category_number = '%1' ").arg(attributeNumber);
            break;
        default:
            break;
    }

    if(query.exec(queryString))
    {
        while(query.next()){
            int number = query.value(0).toInt();
            if (number == 0)
            {
                return false;
            }
        }
    }

    return true;
}

QString InquireSql::InquireAttributeValue(int current_page, QString attributeNumber) const
{
    QSqlQuery query;
    QString queryString;

    if (attributeNumber.isEmpty())
        return NULL;
    switch (current_page)
    {
        case SpecificationPage:
            queryString = QString(" select specifications from specifications_set "
                                " where specifications_number  = '%1' ").arg(attributeNumber);
            break;
        case BrandPage:
            queryString = QString(" select brand from brand_set "
                                " where brand_number  = '%1' ").arg(attributeNumber);
            break;
        case ColorPage:
            queryString = QString(" select color from color_set "
                                " where color_number = '%1' ").arg(attributeNumber);
            break;
        case UnitPage:
            queryString = QString(" select unit from unit_set "
                                " where unit_number  = '%1' ").arg(attributeNumber);
            break;
        case categoryPage:
            queryString = QString(" select category from category_set "
                                " where category_number  = '%1' ").arg(attributeNumber);
            break;
        default:
            break;
    }

    if(query.exec(queryString))
    {
        while(query.next()){
            QString attribute = query.value(0).toString();
            return attribute;
        }
    }
    else
    {
        qDebug() << "InquireAttributeValue error";
    }

    return NULL;
}

supplierList InquireSql::InquireSupplier() const
{
    supplierList list;
    QSqlQuery query;
    QString queryString;

    queryString = QString("select * from supplier");
    if(query.exec(queryString))
    {
        while(query.next()){
            QString supplierNumber =        query.value(0).toString();
            QString ownerName =             query.value(1).toString();
            QString contactName =           query.value(2).toString();
            QString tel =                   query.value(3).toString();
            QString email =                 query.value(4).toString();
            QString supplierBankAccount =   query.value(5).toString();
            QString address =               query.value(6).toString();
            QString supplierRemark =        query.value(7).toString();

            PersonSupplier temp(supplierNumber, ownerName, contactName, tel,
                                email, supplierBankAccount, address, supplierRemark);
            list.append(temp);
        }
    }
    return list;
}

QString InquireSql::InquireSupplierName(QString supplierNumber) const
{
    QSqlQuery query;
    QString queryString;
    queryString = QString(" select owner_name from supplier "
                        " where supplier_number = '%1' ").arg(supplierNumber);

    if(query.exec(queryString))
    {
        while(query.next()){
            QString ownerName = query.value(0).toString();
            return ownerName;
        }
    }
    else
    {
        qDebug() << "InquireOwnerName error";
    }

    return NULL;
}

QString InquireSql::InquireSupplierNumber(QString ownerName) const
{
    QSqlQuery query;
    QString queryString;
    queryString = QString(" select supplier_number from supplier "
                        " where owner_name = '%1' ").arg(ownerName);

    if(query.exec(queryString))
    {
        while(query.next()){
            QString supplierNumber = query.value(0).toString();
            return supplierNumber;
        }
    }
    else
    {
        qDebug() << "InquireOwnerName error";
    }

    return NULL;
}

bool InquireSql::InsertSupplier(const PersonSupplier &supplier) const
{
    QSqlQuery query;
    QString queryString;

    queryString= QString(" insert into supplier values('%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8')")
                                                  .arg(supplier.get_personID(),
                                                       supplier.get_ownerName(),
                                                       supplier.get_personName(),
                                                       supplier.get_personTel(),
                                                       supplier.get_personEmail(),
                                                       supplier.get_supplierBankAccount(),
                                                       supplier.get_personAdress(),
                                                       supplier.get_supplierRemark());
    if(!query.exec(queryString))
    {
        qDebug() << "InsertSupplier " << "no exec!";
        return false;        
    }
    return true;
}

bool InquireSql::UpdateSupplier(const PersonSupplier &supplier) const
{
    QSqlQuery query;
    QString queryString;

    queryString = QString(" update supplier set owner_name = '%2', "
                          "contact_name = '%3', tel = '%4', "
                          "email = '%5', bank_account = '%6',"
                          "address = '%7',  remark = '%8' "
                                      " where supplier_number = '%1' ")
                                                        .arg(supplier.get_personID(),
                                                             supplier.get_ownerName(),
                                                             supplier.get_personName(),
                                                             supplier.get_personTel(),
                                                             supplier.get_personEmail(),
                                                             supplier.get_supplierBankAccount(),
                                                             supplier.get_personAdress(),
                                                             supplier.get_supplierRemark());
    if(!query.exec(queryString))
    {
        qDebug() << "UpdateSupplier no exec!";
        return false;       
    }
    return true;
}

void InquireSql::DeleteSupplier(const QString &supplierNumber) const
{
    QSqlQuery query;
    QString queryString;
    queryString = QString(" delete from supplier "
                                      " where supplier_number = '%1' ").arg(supplierNumber);

    if(!query.exec(queryString))
    {
        qDebug() << "DeleteSupplier no exec!";
    }
}

bool InquireSql::SupplierIsUsed(const QString &supplierNumber) const
{
    QSqlQuery query;
    QString queryString;

    queryString = QString(" select count(*) from purchase_order_form "
                          " where supplier_number = '%1' ").arg(supplierNumber);

    if(query.exec(queryString))
    {
        while(query.next()){
            int number = query.value(0).toInt();
            if (number == 0)
            {
                return false;
            }
        }
    }

    return true;
}

commodityList InquireSql::InquireCommodityInformation() const
{
    commodityList list;
    QSqlQuery query;
    QString queryString;

    queryString = QString("select * from commodity_information");
    if(query.exec(queryString))
    {
        while(query.next()){
            QString commodityBarcode =          query.value(0).toString();
            QString commodityName =             query.value(1).toString();
            QString internalCode =              query.value(2).toString();
            QString categoryNumber =            query.value(3).toString();
            QString specificationsNumber =      query.value(4).toString();
            QString colorNumber =               query.value(5).toString();
            QString brandNumber =               query.value(6).toString();
            QString unitNumber =                query.value(7).toString();
            QString discount =                  query.value(8).toString();
            QString shelfLife =                 query.value(9).toString();
            QString cost =                      query.value(10).toString();
            QString sellingPrice =              query.value(11).toString();
            QString memberPrice =               query.value(12).toString();
            QString stockQuantity =             query.value(13).toString();
            QString minStock =                  query.value(14).toString();
            QString remark =                    query.value(15).toString();

            Commodity temp(commodityBarcode, commodityName, internalCode, categoryNumber,
                                specificationsNumber, colorNumber, brandNumber, unitNumber,
                               discount, shelfLife, cost, sellingPrice,
                               memberPrice, stockQuantity, minStock, remark);
            list.append(temp);
        }
    }
    return list;
}

Commodity InquireSql::InquireCommodityInformationValue(const QString &commodityBarcode) const
{
    QSqlQuery query;
    QString queryString;

    queryString = QString("select * from commodity_information"
                          " where commodity_barcode = '%1' ")
                                        .arg(commodityBarcode);
    if(query.exec(queryString))
    {
        while(query.next()){
            QString commodityBarcode =          query.value(0).toString();
            QString commodityName =             query.value(1).toString();
            QString internalCode =              query.value(2).toString();
            QString categoryNumber =            query.value(3).toString();
            QString specificationsNumber =      query.value(4).toString();
            QString colorNumber =               query.value(5).toString();
            QString brandNumber =               query.value(6).toString();
            QString unitNumber =                query.value(7).toString();
            QString discount =                  query.value(8).toString();
            QString shelfLife =                 query.value(9).toString();
            QString cost =                      query.value(10).toString();
            QString sellingPrice =              query.value(11).toString();
            QString memberPrice =               query.value(12).toString();
            QString stockQuantity =             query.value(13).toString();
            QString minStock =                  query.value(14).toString();
            QString remark =                    query.value(15).toString();

            Commodity temp(commodityBarcode, commodityName, internalCode, categoryNumber,
                                specificationsNumber, colorNumber, brandNumber, unitNumber,
                               discount, shelfLife, cost, sellingPrice,
                               memberPrice, stockQuantity, minStock, remark);
            return temp;
        }
    }

    return Commodity();
}

QString InquireSql::InquireCommodityInternalCode(QString commodityBarcode) const
{
    QSqlQuery query;
    QString queryString;
    queryString = QString(" select internal_code from commodity_information "
                        " where commodity_barcode = '%1' ").arg(commodityBarcode);

    if(query.exec(queryString))
    {
        while(query.next()){
            QString internalCode = query.value(0).toString();
            return internalCode;
        }
    }
    else
    {
        qDebug() << "InquireCommodityInternalCode error";
    }
    return NULL;
}

QString InquireSql::InquireUnitNumber(QString commodityBarcode) const
{
    QSqlQuery query;
    QString queryString;
    queryString = QString(" select unit_number from commodity_information "
                        " where commodity_barcode = '%1' ").arg(commodityBarcode);

    if(query.exec(queryString))
    {
        while(query.next()){
            QString unitNumber = query.value(0).toString();
            return unitNumber;
        }
    }
    else
    {
        qDebug() << "InquireUnitNumber error";
    }
    return NULL;
}

bool InquireSql::InsertCommodityInformation(const Commodity &commodity) const
{
    QSqlQuery query;
    QString queryString, queryString2, queryStringAttribute;

    QString categoryNumber = InquireAttributeNumber(categoryPage, commodity.get_categoryNumber());
    QString brandNumber = InquireAttributeNumber(BrandPage, commodity.get_brandNumbe());
    QString specificationNumber = InquireAttributeNumber(SpecificationPage, commodity.get_specificationsNumber());
    QString colorNumber = InquireAttributeNumber(ColorPage, commodity.get_colorNumber());
    QString unitNumber = InquireAttributeNumber(UnitPage, commodity.get_unitNumber());

    queryString = QString( " insert into commodity_information "
                           " (commodity_barcode, commodity_name, internal_code, "
                           " category_number, unit_number, discount, "
                           " shelf_life, cost, "
                           " selling_price, member_price, stock_quantity, "
                           " min_stock, remark)"
                           " values ('%1', '%2', '%3', '%4', '%5', " )
                                                            .arg(commodity.get_commodityBarcode(),
                                                                 commodity.get_commodityName(),
                                                                 commodity.get_internalCode(),
                                                                 categoryNumber,
                                                                 unitNumber);

    queryString2 = QString(" '%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8') ")
                                                  .arg(commodity.get_discount(),
                                                       commodity.get_shelfLife(),
                                                       commodity.get_cost(),
                                                       commodity.get_sellingPrice(),
                                                       commodity.get_memberPrice(),
                                                       commodity.get_stockQuantity(),
                                                       commodity.get_minStock(),
                                                       commodity.get_remark());

    if(!query.exec(queryString+queryString2))
    {
        qDebug() << "InsertCommodityInformation " << "no exec!";
        return false;
    }

    //插入非必添属性
    if (!specificationNumber.isEmpty())
    {
        queryStringAttribute = QString(" update commodity_information set specifications_number = '%2' "
                          " where commodity_barcode = '%1' ")
                                                              .arg(commodity.get_commodityBarcode(),
                                                                     specificationNumber);

        if(!query.exec(queryStringAttribute))
        {
            qDebug() << "update specification error";
            return false;
        }
    }
    if (!brandNumber.isEmpty())
    {
        queryStringAttribute.clear();
        queryStringAttribute = QString(" update commodity_information set brand_number   = '%2' "
                          " where commodity_barcode = '%1' ")
                                                              .arg(commodity.get_commodityBarcode(),
                                                                     brandNumber);

        if(!query.exec(queryStringAttribute))
        {
            qDebug() << "update brand error";
            return false;
        }
    }
    if (!colorNumber.isEmpty())
    {
        queryStringAttribute.clear();
        queryStringAttribute = QString(" update commodity_information set color_number = '%2' "
                          " where commodity_barcode = '%1' ")
                                                              .arg(commodity.get_commodityBarcode(),
                                                                     colorNumber);

        if(!query.exec(queryStringAttribute))
        {
            qDebug() << "update color error";
            return false;
        }
    }
    return true;
}

bool InquireSql::UpdateCommodityInformation(const Commodity &commodity) const
{
    QSqlQuery query;
    QString queryString, queryString2, queryStringAttribute;

    QString categoryNumber = InquireAttributeNumber(categoryPage, commodity.get_categoryNumber());
    QString brandNumber = InquireAttributeNumber(BrandPage, commodity.get_brandNumbe());
    QString specificationNumber = InquireAttributeNumber(SpecificationPage, commodity.get_specificationsNumber());
    QString colorNumber = InquireAttributeNumber(ColorPage, commodity.get_colorNumber());
    QString unitNumber = InquireAttributeNumber(UnitPage, commodity.get_unitNumber());

    if (!specificationNumber.isEmpty())
    {
        queryStringAttribute = QString(" update commodity_information set specifications_number = '%2' "
                          " where commodity_barcode = '%1' ")
                                                              .arg(commodity.get_commodityBarcode(),
                                                                     specificationNumber);
        if(!query.exec(queryStringAttribute))
        {
            return false;
        }
    }
    if (!brandNumber.isEmpty())
    {
        queryStringAttribute.clear();
        queryStringAttribute = QString(" update commodity_information set brand_number   = '%2' "
                          " where commodity_barcode = '%1' ")
                                                              .arg(commodity.get_commodityBarcode(),
                                                                     brandNumber);
        if(!query.exec(queryStringAttribute))
        {
            return false;
        }
    }
    if (!colorNumber.isEmpty())
    {
        queryStringAttribute.clear();
        queryStringAttribute = QString(" update commodity_information set color_number = '%2' "
                          " where commodity_barcode = '%1' ")
                                                              .arg(commodity.get_commodityBarcode(),
                                                                     colorNumber);
        if(!query.exec(queryStringAttribute))
        {
            return false;
        }
    }
    queryString = QString( " update commodity_information set "
                           " commodity_name = '%1', internal_code = '%2', "
                           " category_number = '%3', unit_number = '%4', " )
                                                                .arg(commodity.get_commodityName(),
                                                                     commodity.get_internalCode(),
                                                                     categoryNumber,
                                                                     unitNumber);

    queryString2 = QString( " discount = '%2',  shelf_life = '%3', "
                            " cost = '%4',  selling_price = '%5', "
                            " member_price = '%6', "
                            " min_stock = '%7',  remark = '%8' "
                            " where commodity_barcode = '%1' " )
                                                                .arg(commodity.get_commodityBarcode(),
                                                                     commodity.get_discount(),
                                                                     commodity.get_shelfLife(),
                                                                     commodity.get_cost(),
                                                                     commodity.get_sellingPrice(),
                                                                     commodity.get_memberPrice(),
                                                                     commodity.get_minStock(),
                                                                     commodity.get_remark());

    if(!query.exec(queryString+queryString2))
    {
        qDebug() << "UpdateCommodityInformation no exec!";
        return false;
    }
    return true;
}

void InquireSql::DeleteCommodityInformation(const QString &commodityBarcode) const
{
    QSqlQuery query;
    QString queryString;
    queryString = QString(" delete from commodity_information "
                                      " where commodity_barcode = '%1' ").arg(commodityBarcode);

    if(!query.exec(queryString))
    {
        qDebug() << "DeleteCommodityInformation no exec!";
    }
}

PurchaseOrderFormList InquireSql::InquirePurchaseOrderForm(QString startDate, QString endDate) const
{
    PurchaseOrderFormList list;
    QSqlQuery query;
    QString queryString;

    if (startDate == "all")
        queryString = QString("select * from purchase_order_form");
    else
        queryString = QString("select * from purchase_order_form where purchase_date between '%1' and '%2'")
                                            .arg(startDate, endDate);
    if(query.exec(queryString))
    {
        while(query.next()){
            QString purchaseDate              = query.value(0).toString();
            QString purchaseOrderFormNumber   = query.value(1).toString();
            QString supplier                  = InquireSupplierName(query.value(2).toString());
            QString staffName                 = InquireStaffName(query.value(3).toString());
            QString remark                    = query.value(4).toString();

            PurchaseOrderForm temp(purchaseDate, purchaseOrderFormNumber, supplier, staffName,
                                remark);
            list.append(temp);
        }
    }
    return list;
}

commodityList InquireSql::InquirePurchaseDetail(QString purchaseOrderFormNumber) const
{
    commodityList list;
    QSqlQuery query;
    QString queryString;

    queryString = QString(" select * from purchase_detail where purchase_order_form_number = '%1' ")
                                            .arg(purchaseOrderFormNumber);
    if(query.exec(queryString))
    {
        while(query.next())
        {

            QString category            = InquireAttributeValue(categoryPage, query.value(3).toString());
            QString specifications      = InquireAttributeValue(SpecificationPage, query.value(4).toString());
            QString color               = InquireAttributeValue(ColorPage, query.value(5).toString());
            QString brand               = InquireAttributeValue(BrandPage, query.value(6).toString());
            QString unit                = InquireAttributeValue(UnitPage, query.value(7).toString());

            QString commodity_barcode   = query.value(0).toString();
            QString commodity_name      = query.value(2).toString();
            QString cost                = query.value(8).toString();
            QString quantitiy           = query.value(9).toString();
            QString subtotal            = query.value(10).toString();

            Commodity temp(commodity_barcode, commodity_name,
                           category, specifications,
                           color, brand, unit,
                           cost, quantitiy,
                           subtotal, purchaseOrderFormNumber);
            list.append(temp);
        }
    }
    else
    {
        qDebug() << "InquirePurchaseDetail error";
    }
    return list;
}

bool InquireSql::InsertPurchaseOrderForm(const PurchaseOrderForm &purchaseOrderForm) const
{
    QSqlQuery query;
    QString queryString;

    QString supplierNumber = InquireSupplierNumber(purchaseOrderForm.get_supplier());

    queryString= QString(" insert into purchase_order_form values('%1', '%2', '%3', '%4', '%5') ")
                                  .arg(purchaseOrderForm.get_purchaseDate(),
                                       purchaseOrderForm.get_purchaseOrderFormNumber(),
                                       supplierNumber,
                                       purchaseOrderForm.get_staff(),
                                       purchaseOrderForm.get_remark());

    if(!query.exec(queryString))
    {
        qDebug() << "InsertPurchaseOrderForm no exec!";
        return false;
    }
    return true;
}

bool InquireSql::InsertpurchaseDetail(const commodityList &purchaseCommodityList) const
{
    QSqlQuery query;
    QString queryString, queryString1;

    //插入进货明细
    for(int i = 0; i < purchaseCommodityList.count(); ++i)
    {
        QString categoryNumber = InquireAttributeNumber(categoryPage,
                                        purchaseCommodityList.at(i).get_categoryNumber());
        QString brandNumber = InquireAttributeNumber(BrandPage,
                                        purchaseCommodityList.at(i).get_brandNumbe());
        QString specificationNumber = InquireAttributeNumber(SpecificationPage,
                                        purchaseCommodityList.at(i).get_specificationsNumber());
        QString colorNumber = InquireAttributeNumber(ColorPage,
                                        purchaseCommodityList.at(i).get_colorNumber());
        QString unitNumber = InquireAttributeNumber(UnitPage,
                                        purchaseCommodityList.at(i).get_unitNumber());
        queryString = QString(" insert into purchase_detail values('%1', '%2', '%3', '%4', '%5', "
                             " '%6', '%7', '%8', ")
                                            .arg(purchaseCommodityList.at(i).get_commodityBarcode(),
                                                 purchaseCommodityList.at(i).get_purchaseOrderFormNumber(),
                                                 purchaseCommodityList.at(i).get_commodityName(),
                                                 categoryNumber,
                                                 specificationNumber,
                                                 colorNumber,
                                                 brandNumber,
                                                 unitNumber);

        queryString1 = QString(" '%1', '%2', '%3') ")
                                            .arg(purchaseCommodityList.at(i).get_cost(),
                                                 purchaseCommodityList.at(i).get_purchaseQuantity(),
                                                 purchaseCommodityList.at(i).get_purchaseTotal());
        if(!query.exec(queryString + queryString1))
        {
            qDebug() << "InsertPurchaseOrderForm no exec!";
            return false;
        }
    }

    //更新库存信息
    for(int i = 0; i < purchaseCommodityList.count(); ++i)
    {
        double nowCost = -1.0;
//        QString nowCostQString;
        int nowStockQuantity = 0;
        queryString = QString("select cost, stock_quantity from commodity_information"
                              " where commodity_barcode = '%1' ")
                                    .arg(purchaseCommodityList.at(i).get_commodityBarcode());
        if(query.exec(queryString))
        {
            while(query.next()){
                double preCost =                      query.value(0).toString().toDouble();
                int preStockQuantity =            query.value(1).toString().toInt();
                double purchaseCost = purchaseCommodityList.at(i).get_cost().toDouble();
                int purchaseQuantity = purchaseCommodityList.at(i).get_purchaseQuantity().toInt();

                nowStockQuantity = preStockQuantity + purchaseQuantity;
                nowCost = (preCost * preStockQuantity + purchaseCost * purchaseQuantity)
                           / (nowStockQuantity);
            }
        }
        else
        {
            return false;
        }

        queryString = QString( " update commodity_information set "
                               " cost = '%2', stock_quantity = '%3' "
                               " where commodity_barcode = '%1' " )
                                    .arg(purchaseCommodityList.at(i).get_commodityBarcode(),
                                         QString::number(nowCost, 'f', 2),
                                         QString::number(nowStockQuantity));

        if(!query.exec(queryString))
            return false;
    }
    return true;
}

bool InquireSql::DeletePurchaseOrderForm(const commodityList & purchaseCommodityList) const
{
    QSqlQuery query;
    QString queryString;

    //更新库存信息
    for(int i = 0; i < purchaseCommodityList.count(); ++i)
    {
        int nowStockQuantity = 0;
        queryString = QString("select stock_quantity from commodity_information"
                              " where commodity_barcode = '%1' ")
                                    .arg(purchaseCommodityList.at(i).get_commodityBarcode());
        if(query.exec(queryString))
        {
            while(query.next()){
                int preStockQuantity =            query.value(0).toString().toInt();
                int purchaseQuantity = purchaseCommodityList.at(i).get_purchaseQuantity().toInt();

                nowStockQuantity = preStockQuantity - purchaseQuantity;
            }
        }
        else
            return false;

        queryString = QString( " update commodity_information set "
                               " stock_quantity = '%2' "
                               " where commodity_barcode = '%1' " )
                                    .arg(purchaseCommodityList.at(i).get_commodityBarcode(),
                                         QString::number(nowStockQuantity));

        if(!query.exec(queryString))
            return false;
    }
    return true;
}

SaleOrderFormList InquireSql::InquireSalesOrderForm(
        QString startDate, QString endDate,
        int inquireManner, QString inquireInformation) const
{
    SaleOrderFormList list;
    QSqlQuery query;
    QString queryString;

    if (inquireInformation.isEmpty())
    {
        if (startDate == "all")
            queryString = QString("select * from sales_order_form");
        else
            queryString = QString("select * from sales_order_form where sale_date between '%1' and '%2'")
                                                .arg(startDate, endDate);
    }
    else
    {
        switch (inquireManner)
        {
            case SalesOrderForm:
            {
                if (startDate == "all")
                    queryString = QString(" select * from sales_order_form "
                                          " where sales_order_form_number = '%1' ")
                                          .arg(inquireInformation);
                else
                    queryString = QString(" select * from sales_order_form "
                                          " where sale_date between '%1' and '%2' "
                                          " and  sales_order_form_number = '%3' ")
                                          .arg(startDate, endDate, inquireInformation);
                break;
            }
            case MemberID:
            {
                if (startDate == "all")
                    queryString = QString(" select * from sales_order_form "
                                          " where membership_nummer = '%1' ")
                                          .arg(inquireInformation);
                else
                    queryString = QString(" select * from sales_order_form "
                                          " where sale_date between '%1' and '%2' "
                                          " and  membership_nummer = '%3' ")
                                          .arg(startDate, endDate, inquireInformation);
                break;
            }
            case staffName:
            {
                if (startDate == "all")
                    queryString = QString(" select * from sales_order_form "
                                          " where staff_id = '%1' ")
                                          .arg(InquireStaffID(inquireInformation));
                else
                    queryString = QString(" select * from sales_order_form "
                                          " where sale_date between '%1' and '%2' "
                                          " and  staff_id = '%3' ")
                                          .arg(startDate, endDate,
                                               InquireStaffID(inquireInformation));
                break;
            }
            default:
                break;
        }
    }


    if (!query.exec(queryString))
    {
        return list;
    }
    else if(query.exec(queryString))
    {
        while(query.next()){
            QString saleOrderFormNumber     = query.value(0).toString();
            QString memberCardNumber        = query.value(1).toString();
            QString memberName              = query.value(2).toString();
            QString quantity                = query.value(3).toString();
            QString totalSales              = query.value(4).toString();
            QString integration             = query.value(5).toString();
            QString memberPayment           = query.value(6).toString();
            QString cashPayment             = query.value(7).toString();
            QString CUPPayment              = query.value(8).toString();
            QString staffName               = InquireStaffName(query.value(9).toString());
            QString salesDate               = query.value(10).toString();

            SaleOrderForm temp(saleOrderFormNumber, memberCardNumber,
                                memberName, quantity, totalSales,
                                integration, memberPayment, cashPayment,
                                CUPPayment, staffName, salesDate);
            list.append(temp);

        }
    }
    return list;
}

saleDetailList InquireSql::InquireSalesDetail(QString salesOrderFormNumber) const
{
    saleDetailList list;
    QSqlQuery query;
    QString queryString;

    queryString = QString(" select * from sales_detail "
                          " where sales_order_form_number = '%1' ")
                                            .arg(salesOrderFormNumber);

    if(query.exec(queryString))
    {

        while(query.next())
        {
            QString commodityBarcode    = query.value(0).toString();
            QString commodityName       = query.value(2).toString();
            QString internalCode        = query.value(3).toString();
            QString unitValue           = InquireAttributeValue(UnitPage, query.value(4).toString());

            QString sellingPrice        = query.value(5).toString();
            QString realDiscount        = query.value(6).toString();
            QString realSellingPrice    = query.value(7).toString();
            QString saleQuantity        = query.value(8).toString();
            QString saleSum             = query.value(9).toString();

            bool integration;
            if (query.value(10).toString() == "yes")
                integration = true;
            else
                integration = false;

            QString saleStaffName       = InquireStaffName(query.value(11).toString());

            SaleDetail temp(commodityBarcode, commodityName,
                           internalCode, unitValue,
                           sellingPrice, realDiscount, realSellingPrice,
                           saleQuantity, saleSum,
                           integration, saleStaffName);
            list.append(temp);
        }
    }
    else
    {
        qDebug() << "InquireSalesDetail error";
    }
    return list;
}

saleDetailList InquireSql::InquireSalesList(
    QString startDate, QString endDate,
    int inquireManner, QString inquireInformation) const
{
    saleDetailList list;
    QSqlQuery query;
    QString queryString;

    if (inquireInformation.isEmpty())
    {
        if (startDate == "all")
            queryString = QString(" select * from sales_detail ");
        else
            queryString = QString(" select * from sales_detail "
                                  " where sale_date "
                                  " between '%1' and '%2' ")
                                                .arg(startDate, endDate);
    }
    else
    {
        switch (inquireManner)
        {
            case SalesOrderForm:
            {
                if (startDate == "all")
                    queryString = QString(" select * from sales_detail "
                                          " where sales_order_form_number = '%1' ")
                                          .arg(inquireInformation);
                else
                    queryString = QString(" select * from sales_detail "
                                          " where sale_date "
                                          " between '%1' and '%2' "
                                          " and sales_order_form_number = '%3' ")
                                          .arg(startDate, endDate, inquireInformation);
                break;
            }
            case MemberID:
            {
                if (startDate == "all")
                    queryString = QString(" select * from sales_detail "
                                          " where sales_order_form_number = "
                                          " (select sales_order_form_number from sales_order_form "
                                          " where membership_nummer = '%1') ")
                                          .arg(inquireInformation);
                else
                    queryString = QString(" select * from sales_detail "
                                          " where sale_date "
                                          " between '%1' and '%2' "
                                          " and (sales_order_form_number = "
                                          " (select sales_order_form_number from sales_order_form "
                                          " where membership_nummer = '%3')) ")
                                          .arg(startDate, endDate, inquireInformation);
                break;
            }
            case staffName:
            {
                if (startDate == "all")
                    queryString = QString(" select * from sales_detail "
                                          " where staff_id = '%1' ")
                                          .arg(InquireStaffID(inquireInformation));
                else
                    queryString = QString(" select * from sales_detail "
                                          " where sale_date between '%1' and '%2' "
                                          " and  staff_id = '%3' ")
                                          .arg(startDate, endDate,
                                               InquireStaffID(inquireInformation));
                break;
            }
            default:
                break;
        }
    }

    if (!query.exec(queryString))
    {
        return list;
    }
    else if(query.exec(queryString))
    {
        while(query.next()){
            QString commodityBarcode    = query.value(0).toString();
            QString commodityName       = query.value(2).toString();
            QString internalCode        = query.value(3).toString();
            QString unitValue           = InquireAttributeValue(UnitPage, query.value(4).toString());

            QString sellingPrice        = query.value(5).toString();
            QString realDiscount        = query.value(6).toString();
            QString realSellingPrice    = query.value(7).toString();
            QString saleQuantity        = query.value(8).toString();
            QString saleSum             = query.value(9).toString();

            bool integration;
            if (query.value(10).toString() == "yes")
                integration = true;
            else
                integration = false;

            QString saleStaffName       = InquireStaffName(query.value(11).toString());

            SaleDetail temp(commodityBarcode, commodityName,
                           internalCode, unitValue,
                           sellingPrice, realDiscount, realSellingPrice,
                           saleQuantity, saleSum,
                           integration, saleStaffName);
            list.append(temp);
        }
    }
    return list;
}

bool InquireSql::InsertSalesOrderForm(const SaleOrderForm &salesOrderForm) const
{
    QSqlQuery query;
    QString queryString, queryString2;

    queryString = QString(" insert into sales_order_form values('%1', '%2', '%3', '%4', '%5', "
                         " '%6', '%7', '%8', ")
                        .arg(salesOrderForm.get_saleOrderFormNumber(),
                             salesOrderForm.get_memberCardNumber(),
                             salesOrderForm.get_memberName(),
                             salesOrderForm.get_quantity(),
                             salesOrderForm.get_totalSales(),
                             salesOrderForm.get_integration(),
                             salesOrderForm.get_memberPayment(),
                             salesOrderForm.get_cashPayment());

    queryString2 = QString(" '%1', '%2', '%3') ")
                         .arg(salesOrderForm.get_CUPPayment(),
                              salesOrderForm.get_staffName(),
                              salesOrderForm.get_salesDate());

    if(!query.exec(queryString+queryString2))
    {
        qDebug() << "InsertSalesOrderForm no exec!";
        return false;
    }
    return true;
}

bool InquireSql::InsertSalesDetail(const QString & salesOrderFormNumber,
                                   const saleDetailList & SalesDetailList) const
{
    QSqlQuery query;
    QString queryString, queryString1;

    //插入销售明细
    for(int i = 0; i < SalesDetailList.count(); ++i)
    {
        QString integration;
        if (SalesDetailList.at(i).get_integration())
            integration = "yes";
        else
            integration = "no";

//        ui->tableWidgetSale->setItem(i, 0, NO);
//        ui->tableWidgetSale->setItem(i, 1, commodityBarcode);
//        ui->tableWidgetSale->setItem(i, 2, commodityName);
//        ui->tableWidgetSale->setItem(i, 3, original);
//        ui->tableWidgetSale->setItem(i, 4, commoditydDiscount);
//        ui->tableWidgetSale->setItem(i, 5, quantity);
//        ui->tableWidgetSale->setItem(i, 6, sellingPrice);
//        ui->tableWidgetSale->setItem(i, 7, saleSum);
//        ui->tableWidgetSale->setCellWidget(i, 8, integration);

//    SaleDetail(const QString commodityBarcode, const QString commodityName, const QString cost,
//               const QString discount, const QString saleQuantity,  const QString sellingPrice,
//               const QString memberPrice, const QString realSellingPrice, const QString saleSum,
//               const QString saleStaffName, const bool integration, const QString stockQuantity,
//               const QString originalPrice)

        queryString = QString(" insert into sales_detail values('%1', '%2', '%3', '%4', '%5', "
                             " '%6', '%7', '%8', ")
                            .arg(SalesDetailList.at(i).get_commodityBarcode(),
                                 salesOrderFormNumber,
                                 SalesDetailList.at(i).get_commodityName(),
                                 InquireCommodityInternalCode(
                                     SalesDetailList.at(i).get_commodityBarcode()),
                                 InquireUnitNumber(
                                     SalesDetailList.at(i).get_commodityBarcode()),
                                 SalesDetailList.at(i).get_sellingPrice(),
                                 SalesDetailList.at(i).get_discount(),
                                 SalesDetailList.at(i).get_realSellingPrice());

        queryString1 = QString(" '%1', '%2', '%3', '%4', '%5') ")
                             .arg(SalesDetailList.at(i).get_saleQuantity(),
                                  SalesDetailList.at(i).get_saleSum(),
                                  integration,
                                  InquireStaffID(SalesDetailList.at(i).get_saleStaffName()),
                                  QDateTime::currentDateTime().toString("yyyy-MM-dd"));

        if(!query.exec(queryString + queryString1))
        {
            qDebug() << "InsertSalesDetail no exec!";
            return false;
        }
    }

    //更新库存信息
    for(int i = 0; i < SalesDetailList.count(); ++i)
    {
        int nowStockQuantity = -1;
        queryString = QString("select stock_quantity from commodity_information"
                              " where commodity_barcode = '%1' ")
                                    .arg(SalesDetailList.at(i).get_commodityBarcode());
        if(query.exec(queryString))
        {
            while(query.next()){
                int preStockQuantity = query.value(0).toString().toInt();
                int salesQuantity = SalesDetailList.at(i).get_saleQuantity().toInt();

                nowStockQuantity = preStockQuantity - salesQuantity;
            }
        }
        else
            return false;

        queryString = QString( " update commodity_information set "
                               " stock_quantity = '%2' "
                               " where commodity_barcode = '%1' " )
                                    .arg(SalesDetailList.at(i).get_commodityBarcode(),
                                         QString::number(nowStockQuantity));

        if(!query.exec(queryString))
            return false;
    }
    return true;
}

memberList InquireSql::InquireMemberProfile() const
{
    memberList list;
    QSqlQuery query;
    QString queryString;

    queryString = QString("select * from member_profile");
    if(query.exec(queryString))
    {
        while(query.next()){
//            InquireMemberLevelValue(query.value(3).toString());
            QString membercardID =          query.value(0).toString();
            QString memberName =            query.value(1).toString();
            QString gender =                query.value(2).toString();
//            QString memberLevel =           query.value(3).toString();
            QString memberLevel =           InquireMemberLevelValue(query.value(3).toString());
            QString membercardAmount =      query.value(4).toString();
            QString integration =           query.value(5).toString();
            QString discount =              query.value(6).toString();
            QString totalSpending =         query.value(7).toString();
            QString consumeTimes =          query.value(8).toString();
            QString handleDate =            query.value(9).toString();
            QString tel =                   query.value(10).toString();
            QString email =                 query.value(11).toString();
            QString address =               query.value(12).toString();
            QString memberRemark =          query.value(13).toString();
            QString membercardPassword =    query.value(14).toString();
            QString handelStaffName =       query.value(15).toString();

            PersonMember temp(membercardID, memberName, gender,
                         memberLevel, membercardAmount, integration,
                         discount, totalSpending, consumeTimes,
                         handleDate, tel, email,
                         address, memberRemark,
                         membercardPassword, handelStaffName);

            list.append(temp);
        }
    }
    return list;
}

PersonMember InquireSql::InquireMemberInformation(const QString &membercardID) const
{
    QSqlQuery query;
    QString queryString;

    queryString = QString("select * from member_profile"
                          " where membership_id = '%1' ").arg(membercardID);
    if(query.exec(queryString))
    {
        while(query.next()){
            QString membercardID =          query.value(0).toString();
            QString memberName =            query.value(1).toString();
            QString gender =                query.value(2).toString();
//            QString memberLevel =           query.value(3).toString();
            QString memberLevel =           InquireMemberLevelValue(query.value(3).toString());
            QString membercardAmount =      query.value(4).toString();
            QString integration =           query.value(5).toString();
            QString discount =              query.value(6).toString();
            QString totalSpending =         query.value(7).toString();
            QString consumeTimes =          query.value(8).toString();
            QString handleDate =            query.value(9).toString();
            QString tel =                   query.value(10).toString();
            QString email =                 query.value(11).toString();
            QString address =               query.value(12).toString();
            QString memberRemark =          query.value(13).toString();
            QString membercardPassword =    query.value(14).toString();
            QString handelStaffName =       query.value(15).toString();

            PersonMember personMember(membercardID, memberName, gender,
                         memberLevel, membercardAmount, integration,
                         discount, totalSpending, consumeTimes,
                         handleDate, tel, email,
                         address, memberRemark,
                         membercardPassword, handelStaffName);
            return personMember;
        }
    }

    return PersonMember();
}

bool InquireSql::InsertMemberInformation(const PersonMember &memberInformation) const
{
    QSqlQuery query;
    QString queryString, queryString2;

    QString memberLevelNumber;
    if (memberInformation.get_memberLevel().isEmpty())
    {
        memberLevelNumber = "";
    }else{
        memberLevelNumber = InquireMemberLevelNumber(memberInformation.get_memberLevel());
    }

    QString staffID = InquireStaffID(memberInformation.get_handelStaffName());

    queryString = QString( " insert into member_profile values "
                           " ('%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8', '%9'," )
                                    .arg(memberInformation.get_personID(),
                                         memberInformation.get_personName(),
                                         memberInformation.get_personGender(),
                                         memberLevelNumber,
                                         memberInformation.get_membercardAmount(),
                                         memberInformation.get_integration(),
                                         memberInformation.get_discount(),
                                         "0", "0");

    queryString2 = QString(" '%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8', '%9') ")
                                    .arg(memberInformation.get_handleDate(),
                                         memberInformation.get_personTel(),
                                         memberInformation.get_personEmail(),
                                         memberInformation.get_personAdress(),
                                         memberInformation.get_memberRemark(),
                                         memberInformation.get_membercardPassword(),
                                         staffID,
                                         memberInformation.get_cashPayment(),
                                         memberInformation.get_CUPPayment());

    if(!query.exec(queryString+queryString2))
    {
        qDebug() << "InsertMemberInformation " << "no exec!";
        return false;
    }else{
        return true;
    }
}

QString InquireSql::inquireMemberPassword(const QString &memberPassword) const
{
    QSqlQuery query;
    QString queryString = QString("Select membercard_password from member_profile "
                                  " where membership_id = '%1' ").arg(memberPassword);
    QString paswd;
    if(query.exec(queryString)){

        while(query.next()){
            paswd = query.value(0).toString();
        }
    }
    return paswd;
}

bool InquireSql::UpdateMemberPassword(const QString &membercardID, const QString &memberPassword) const
{
    QSqlQuery query;
    QString queryString = QString(" update member_profile set "
                                  " membercard_password = '%2' "
                                  " where membership_id = '%1' ").arg(membercardID, memberPassword);

    if(!query.exec(queryString))
        return false;
    return true;
}

bool InquireSql::UpdatMemberInformation(const PersonMember &personMember) const
{
    QSqlQuery query;
    QString queryString = QString(" update member_profile set "
                                  " member_name = '%2', gender = '%3', "
                                  " tel = '%4', email = '%5', "
                                  " address = '%6', remark = '%7' "
                                  " where membership_id = '%1' ")
            .arg(personMember.get_personID(),
                 personMember.get_personName(),
                 personMember.get_personGender(),
                 personMember.get_personTel(),
                 personMember.get_personEmail(),
                 personMember.get_personAdress(),
                 personMember.get_memberRemark());

    if(!query.exec(queryString))
        return false;
    return true;
}

bool InquireSql::UpdatMemberOver(const PersonMember &memberOver) const
{
    QSqlQuery query;

    QString memberLevelNumber;
    if (memberOver.get_memberLevel().isEmpty())
    {
        memberLevelNumber = "";
    }else{
        memberLevelNumber = InquireMemberLevelNumber(memberOver.get_memberLevel());
    }

    QString queryString = QString(" update member_profile set "
                                  " member_level_number = '%2', discount = '%3', "
                                  " integration = '%4', membercard_amount = '%5', "
                                  " total_spending = '%6', consume_times = '%7' "
                                  " where membership_id = '%1' ")
            .arg(memberOver.get_personID(),
                 memberLevelNumber,
                 memberOver.get_discount(),
                 memberOver.get_integration(),
                 memberOver.get_membercardAmount(),
                 memberOver.get_totalSpending(),
                 memberOver.get_consumeTimes());

    if(!query.exec(queryString))
        return false;
    return true;
}

void InquireSql::DeleteMemberInformation(const QString &membercardID) const
{
    QSqlQuery query;
    QString queryString;
    queryString = QString(" delete from member_profile "
                                      " where membership_id = '%1' ").arg(membercardID);

    if(!query.exec(queryString))
    {
        qDebug() << "DeleteSupplier no exec!";
    }
}

memberList InquireSql::InquireMemberLevel() const
{
    memberList list;
    QSqlQuery query;
    QString queryString;

    queryString = QString("select * from member_level");
    if(query.exec(queryString))
    {
        while(query.next()){
            QString memberLevelNumber =     query.value(0).toString();
            QString memberLevel =           query.value(1).toString();
            QString discount =              query.value(2).toString();
            QString membercardAmount =      query.value(3).toString();
            QString integration =           query.value(4).toString();

            PersonMember temp(memberLevelNumber, memberLevel, discount, membercardAmount, integration);

            list.append(temp);
        }
    }
    return list;
}

bool InquireSql::InsertMemberLevel(const PersonMember &memberLevel) const
{
    QSqlQuery query;
    QString queryString;

    queryString= QString(" insert into member_level values('%1', '%2', '%3', '%4', '%5') ")
                              .arg(memberLevel.get_memberLevelNumber(),
                                   memberLevel.get_memberLevel(),
                                   memberLevel.get_discount(),
                                   memberLevel.get_membercardAmount(),
                                   memberLevel.get_integration());
    if(!query.exec(queryString))
    {
        qDebug() << "InsertMemberLevel " << "no exec!";
        return false;
    }
    return true;
}

bool InquireSql::UpdateMemberLevel(const PersonMember &memberLevel) const
{
    QSqlQuery query;
    QString queryString;

    queryString = QString(" update member_level set member_level = '%2', "
                          " discount = '%3', membercard_amount = '%4', "
                          " integration = '%5' "
                          " where member_level_number = '%1' ")
                                                        .arg(memberLevel.get_memberLevelNumber(),
                                                             memberLevel.get_memberLevel(),
                                                             memberLevel.get_discount(),
                                                             memberLevel.get_membercardAmount(),
                                                             memberLevel.get_integration());
    if(!query.exec(queryString))
    {
        qDebug() << "UpdateMemberLevel no exec!";
        return false;
    }
    return true;
}

void InquireSql::DeleteMemberLevel(const QString &memberLevel) const
{
    QSqlQuery query;
    QString queryString;
    queryString = QString(" delete from member_level "
                          " where member_level = '%1' ").arg(memberLevel);

    if(!query.exec(queryString))
    {
        qDebug() << "DeleteMemberLevel no exec!";
    }
}

QString InquireSql::InquireMemberLevelValue(QString memberLevelNumber) const
{
    if (memberLevelNumber.isEmpty())
        return "";
    QSqlQuery query;
    QString queryString = QString("Select member_level From member_level where member_level_number = '%1'")
                                                    .arg(memberLevelNumber);

    if(query.exec(queryString))
    {

        while(query.next()){
            QString memberLevel = query.value(0).toString();
            return memberLevel;
        }
    }

    return NULL;
}

QStringList InquireSql::inquireAllMemberLevelValue() const
{
    QSqlQuery query;
    QString queryString = QString("Select member_level From member_level");

    QStringList memberLevelList;
    if(query.exec(queryString)){

        while(query.next()){
            QString memberLevel = query.value(0).toString();
            memberLevelList.append(memberLevel);
        }
    }
    return memberLevelList;
}

QString InquireSql::InquireMemberLevelNumber(const QString& MemberLevel) const
{
    if (MemberLevel.isEmpty())
    {
        return NULL;

    }

    QSqlQuery query;
    QString queryString;

    queryString = QString(" select member_level_number from member_level "
                        " where member_level = '%1' ").arg(MemberLevel);

    if(query.exec(queryString))
    {
        while(query.next()){
            QString MemberLevelNumber = query.value(0).toString();
            return MemberLevelNumber;
        }
    }
    else
    {
        qDebug() << "InquireMemberLevelNumber no exec!";
    }

    return NULL;
}

bool InquireSql::UpdatMemberCardAmount(const QString &memberCardNumber, const QString &amount) const
{
    QSqlQuery query;
    QString queryString;
    QString membercardAmount;

    queryString = QString(" select membercard_amount from member_profile "
                        " where membership_id = '%1' ").arg(memberCardNumber);
    if(query.exec(queryString))
    {
        while(query.next()){
            membercardAmount = query.value(0).toString();
        }
    }

    queryString = QString(" update member_profile set membercard_amount = '%2' "
                          " where membership_id = '%1' ")
                            .arg(memberCardNumber,
                            QString::number(amount.toDouble() + membercardAmount.toDouble()));

    if(!query.exec(queryString))
    {
        qDebug() << "UpdateMemberLevel no exec!";
        return false;
    }
    return true;
}

MemberOperationDetailsList InquireSql::InquireMemberOperationDetails(QString startDate, QString endDate, int inquireManner, QString inquireInformation) const
{
    MemberOperationDetailsList list;
    QSqlQuery query;
    QString queryString;

    if (inquireInformation.isEmpty())
    {
        if (startDate == "all")
            queryString = QString("select * from member_operation_details");
        else
            queryString = QString("select * from member_operation_details where consumption_date between '%1' and '%2'")
                                                .arg(startDate, endDate);
    }
    else
    {
        switch (inquireManner)
        {
            case MemberCardID:
            {
                if (startDate == "all")
                    queryString = QString(" select * from member_operation_details "
                                          " where membership_id = '%1' ")
                                          .arg(inquireInformation);
                else
                    queryString = QString(" select * from member_operation_details "
                                          " where consumption_date between '%1' and '%2' "
                                          " and  membership_id = '%3' ")
                                          .arg(startDate, endDate, inquireInformation);
                break;
            }
            case MemberName:
            {
                if (startDate == "all")
                    queryString = QString(" select * from member_operation_details "
                                          " where member_name = '%1' ")
                                          .arg(inquireInformation);
                else
                    queryString = QString(" select * from member_operation_details "
                                          " where consumption_date between '%1' and '%2' "
                                          " and  member_name = '%3' ")
                                          .arg(startDate, endDate, inquireInformation);
                break;
            }

//            select * from member_operation_details
//            where membership_id = (select membership_id from member_profile where tel = '15035154212');
            case Tel:
            {
                if (startDate == "all")
                    queryString = QString(" select * from member_operation_details "
                        " where membership_id = "
                        " (select membership_id from member_profile where tel = '%1') ")
                        .arg(inquireInformation);
                else
                    queryString = QString(" select * from member_operation_details "
                        " where consumption_date between '%1' and '%2' "
                        " and membership_id = "
                        " (select membership_id from member_profile where tel = '%3') ")
                        .arg(startDate, endDate, inquireInformation);
                break;
            }
            default:
                break;
        }
    }

    if(query.exec(queryString))
    {
        while(query.next()){
            QString memberID            = query.value(0).toString();
            QString memberName          = query.value(1).toString();
            QString memberLevel         = InquireMemberLevelValue(query.value(2).toString());
            QString consumptionType     = query.value(3).toString();
            QString consumptionTotal    = query.value(4).toString();
            QString integration         = query.value(5).toString();
            QString cashPayment         = query.value(6).toString();
            QString cupPayment          = query.value(7).toString();
            QString staffName           = InquireStaffName(query.value(8).toString());
            QString consumptionDate     = query.value(9).toString();

            PersonMemberOperationDetails temp(memberID, memberName, memberLevel, consumptionType,
                                consumptionTotal, integration, cashPayment, cupPayment,
                                staffName, consumptionDate);
            list.append(temp);
        }
    }
    return list;
}

bool InquireSql::InsertMemberOperationDetails(const PersonMemberOperationDetails &memberOperationDetails) const
{
    QSqlQuery query;
    QString queryString, queryString2;

    queryString = QString(" insert into member_operation_details "
                         " values('%1', '%2', '%3', '%4', '%5', ")
                                  .arg(memberOperationDetails.get_personID(),
                                       memberOperationDetails.get_personName(),
                                       InquireMemberLevelNumber(memberOperationDetails.get_memberLevel()),
                                       memberOperationDetails.get_consumptionType(),
                                       memberOperationDetails.get_consumptionTotal());

    queryString2 = QString(" '%1', '%2', '%3', '%4', '%5') ")
                                    .arg(memberOperationDetails.get_integration(),
                                         memberOperationDetails.get_cashPayment(),
                                         memberOperationDetails.get_cupPayment(),
                                         InquireStaffID(memberOperationDetails.get_staffName()),
                                         memberOperationDetails.get_consumptionDate());

    if(!query.exec(queryString+queryString2))
    {
        qDebug() << "InsertMemberOperationDetails no exec!";
        return false;
    }
    return true;
}

SystemSet InquireSql::InquireSystemSettingsInformation() const
{
    QSqlQuery query;
    QString queryString;

    queryString = QString("select * from system_settings");
    if(query.exec(queryString))
    {
        while(query.next()){
            QString title                       = query.value(0).toString();
            bool inventoryShortagesProhibitSale = query.value(1).toInt();
            bool inventoryShortagesPrompts      = query.value(2).toInt();
            bool useMemberPassword              = query.value(3).toInt();
            int integrationMode                 = query.value(4).toInt();
            bool forbidModifyPricesDiscounts    = query.value(5).toInt();
            bool printMemberIntegration         = query.value(6).toInt();
            bool modifyWindowPopsUp             = query.value(7).toInt();
            bool printCommodityBarcode          = query.value(8).toInt();
            bool autoAddCommodityQuantity       = query.value(9).toInt();
            bool printSellingPrice              = query.value(10).toInt();
            bool useDeleteOrderFormProtection   = query.value(11).toInt();
            QString deleteOrderFormPassword     = query.value(12).toString();
            int memberPrice                     = query.value(13).toInt();
            int generalCustomerPrice            = query.value(14).toInt();

            SystemSet systemSettingsInformation(title, inventoryShortagesProhibitSale,
                         inventoryShortagesPrompts, useMemberPassword,
                         integrationMode, forbidModifyPricesDiscounts, printMemberIntegration,
                         modifyWindowPopsUp, printCommodityBarcode, autoAddCommodityQuantity,
                         printSellingPrice, useDeleteOrderFormProtection, deleteOrderFormPassword,
                         memberPrice, generalCustomerPrice);
            return systemSettingsInformation;
        }
    }

    return SystemSet();
}

bool InquireSql::UpdateSystemSettingsInformation(const SystemSet &systemSettingsInformation) const
{
    QSqlQuery query;
    QString queryString = QString(" delete from system_settings ");
    if(!query.exec(queryString))
        return false;
//    if ()
    queryString = QString(" insert into system_settings values ( "
                  " '%1', %2, %3, %4, '%5', %6, %7, %8, ")
           .arg(systemSettingsInformation.get_title(),
                QString::number(systemSettingsInformation.get_inventoryShortagesProhibitSale()),
                QString::number(systemSettingsInformation.get_inventoryShortagesPrompts()),
                QString::number(systemSettingsInformation.get_useMemberPassword()),
                QString::number(systemSettingsInformation.get_integrationMode()),
                QString::number(systemSettingsInformation.get_forbidModifyPricesDiscounts()),
                QString::number(systemSettingsInformation.get_printMemberIntegration()),
                QString::number(systemSettingsInformation.get_modifyWindowPopsUp()));

    QString queryString2 = QString(" %1, %2, %3, %4, '%5', %6, %7) ")
           .arg(QString::number(systemSettingsInformation.get_printCommodityBarcode()),
                QString::number(systemSettingsInformation.get_autoAddCommodityQuantity()),
                QString::number(systemSettingsInformation.get_printSellingPrice()),
                QString::number(systemSettingsInformation.get_useDeleteOrderFormProtection()),
                systemSettingsInformation.get_deleteOrderFormPassword(),
                QString::number(systemSettingsInformation.get_memberPrice()),
                QString::number(systemSettingsInformation.get_generalCustomerPrice()));

    if(!query.exec(queryString + queryString2))
        return false;
    return true;
}

QStringList InquireSql::InventoryStatistics() const
{
    QSqlQuery query;
    QString queryString = QString(" select sum(stock_quantity), "
                                  " sum(cost) from commodity_information ");

    QStringList inventoryStatistics;
    if(query.exec(queryString)){

        while(query.next()){
            QString stockQuantity   = query.value(0).toString();
            QString costTotal   = query.value(1).toString();
            inventoryStatistics.append(stockQuantity);
            inventoryStatistics.append(costTotal);
        }
    }
    return inventoryStatistics;
}

QStringList InquireSql::MemberOperationDetailsStatistics(
        QString temp, QString temp2, QString startDate, QString endDate,
        int inquireManner, QString inquireInformation) const
{
    QSqlQuery query;
    QString queryString;

    //办卡业绩
    if (inquireInformation.isEmpty())
    {
        if (startDate == "all")
            queryString = QString(" select sum(consumption_total) "
                                  " from member_operation_details "
                                  " where consumption_type = '%1' ")
                                  .arg(temp);
        else
            queryString = QString(" select sum(consumption_total) "
                                  " from member_operation_details "
                                  " where consumption_type = '%1' and "
                                  " consumption_date between '%2' and '%3'")
                                  .arg(temp, startDate, endDate);
    }
    else
    {
        switch (inquireManner)
        {
            case MemberCardID:
            {
                if (startDate == "all")
                    queryString = QString(" select sum(consumption_total) "
                                          " from member_operation_details "
                                          " where consumption_type = '%1' "
                                          " and membership_id = '%2' ")
                                          .arg(temp, inquireInformation);
                else
                    queryString = QString(" select sum(consumption_total) "
                                          " from member_operation_details "
                                          " where consumption_type = '%1' and "
                                          " (consumption_date between '%2' and '%3') "
                                          " and membership_id = '%4' ")
                                          .arg(temp, startDate,
                                               endDate, inquireInformation);
                break;
            }
            case MemberName:
            {
                if (startDate == "all")
                    queryString = QString(" select sum(consumption_total) "
                                          " from member_operation_details "
                                          " where consumption_type = '%1' "
                                          " and member_name = '%2' ")
                                          .arg(temp, inquireInformation);
                else
                    queryString = QString(" select sum(consumption_total) "
                                          " from member_operation_details "
                                          " where consumption_type = '%1' and "
                                          " (consumption_date between '%2' and '%3') "
                                          " and member_name = '%4' ")
                                          .arg(temp, startDate,
                                               endDate, inquireInformation);
                break;
            }
            case Tel:
            {
                if (startDate == "all")
                    queryString = QString(" select sum(consumption_total) "
                                          " from member_operation_details "
                                          " where consumption_type = '%1' "
                                          " and membership_id = "
                                          " (select membership_id from member_profile where tel = '%2') ")
                                          .arg(temp, inquireInformation);
                else
                    queryString = QString(" select sum(consumption_total) "
                                          " from member_operation_details "
                                          " where consumption_type = '%1' and "
                                          " (consumption_date between '%2' and '%3') "
                                          " and membership_id = "
                                          " (select membership_id from member_profile where tel = '%4') ")
                                          .arg(temp, startDate,
                                               endDate, inquireInformation);
                break;
            }
            default:
                break;
        }
    }

    QStringList memberOperationDetailsStatistics;
    if(query.exec(queryString)){

        while(query.next()){
            QString handleResult = query.value(0).toString();
            memberOperationDetailsStatistics.append(handleResult);
        }
    }

    //充值业绩
    if (inquireInformation.isEmpty())
    {
        if (startDate == "all")
            queryString = QString(" select sum(consumption_total) "
                                  " from member_operation_details "
                                  " where consumption_type = '%1' ")
                                  .arg(temp2);
        else
            queryString = QString(" select sum(consumption_total) "
                                  " from member_operation_details "
                                  " where consumption_type = '%1' and "
                                  " consumption_date between '%2' and '%3'")
                                  .arg(temp2, startDate, endDate);
    }
    else
    {
        switch (inquireManner)
        {
            case MemberCardID:
            {
                if (startDate == "all")
                    queryString = QString(" select sum(consumption_total) "
                                          " from member_operation_details "
                                          " where consumption_type = '%1' "
                                          " and membership_id = '%2' ")
                                          .arg(temp2, inquireInformation);
                else
                    queryString = QString(" select sum(consumption_total) "
                                          " from member_operation_details "
                                          " where consumption_type = '%1' and "
                                          " (consumption_date between '%2' and '%3') "
                                          " and membership_id = '%4' ")
                                          .arg(temp2, startDate,
                                               endDate, inquireInformation);
                break;
            }
            case MemberName:
            {
                if (startDate == "all")
                    queryString = QString(" select sum(consumption_total) "
                                          " from member_operation_details "
                                          " where consumption_type = '%1' "
                                          " and member_name = '%2' ")
                                          .arg(temp2, inquireInformation);
                else
                    queryString = QString(" select sum(consumption_total) "
                                          " from member_operation_details "
                                          " where consumption_type = '%1' and "
                                          " (consumption_date between '%2' and '%3') "
                                          " and member_name = '%4' ")
                                          .arg(temp2, startDate,
                                               endDate, inquireInformation);
                break;
            }
            case Tel:
            {
                if (startDate == "all")
                    queryString = QString(" select sum(consumption_total) "
                                          " from member_operation_details "
                                          " where consumption_type = '%1' "
                                          " and membership_id = "
                                          " (select membership_id from member_profile where tel = '%2') ")
                                          .arg(temp2, inquireInformation);
                else
                    queryString = QString(" select sum(consumption_total) "
                                          " from member_operation_details "
                                          " where consumption_type = '%1' and "
                                          " (consumption_date between '%2' and '%3') "
                                          " and membership_id = "
                                          " (select membership_id from member_profile where tel = '%4') ")
                                          .arg(temp2, startDate,
                                               endDate, inquireInformation);
                break;
            }
            default:
                break;
        }
    }

    if(query.exec(queryString)){

        while(query.next()){
            QString rechargeResult = query.value(0).toString();
            memberOperationDetailsStatistics.append(rechargeResult);
        }
    }

    return memberOperationDetailsStatistics;
}

QString InquireSql::InquireAttributeNumber(int currentPage, const QString & attributeValue) const
{
    QSqlQuery query;
    QString queryString;

    if (attributeValue.isEmpty())
        return NULL;
    switch (currentPage)
    {
        case SpecificationPage:
            queryString = QString(" select specifications_number from specifications_set "
                                " where specifications = '%1' ").arg(attributeValue);
            break;
        case BrandPage:
            queryString = QString(" select brand_number from brand_set "
                                " where brand = '%1' ").arg(attributeValue);
            break;
        case ColorPage:
            queryString = QString(" select color_number from color_set "
                                " where color = '%1' ").arg(attributeValue);
            break;
        case UnitPage:
            queryString = QString(" select unit_number from unit_set "
                                " where unit = '%1' ").arg(attributeValue);
            break;
        case categoryPage:
            queryString = QString(" select category_number from category_set "
                                " where category = '%1' ").arg(attributeValue);
            break;
        default:
            break;
    }

    if(query.exec(queryString))
    {
        while(query.next()){
            QString attributeNumber = query.value(0).toString();
            return attributeNumber;
        }
    }
    else
    {
        qDebug() << "InquireAttributeNumber error";
    }

    return NULL;
}
